package com.lkd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.SkuPageDto;
import com.lkd.entity.Sku;
import com.lkd.mapper.SkuClassMapper;
import com.lkd.mapper.SkuMapper;
import com.lkd.result.PageBean;
import com.lkd.service.SkuService;
import com.lkd.vo.SkuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl implements SkuService {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuClassMapper skuClassMapper;

    //新增商品
    @Override
    public Boolean save(Sku sku) {
        sku.setIsDiscount(false);
        sku.setCreateTime(LocalDateTime.now());
        sku.setUpdateTime(LocalDateTime.now());
        Boolean result = skuMapper.save(sku);
        return result;
    }

    //商品分页查询
    @Override
    public PageBean getPage(SkuPageDto skuPageDto) {
        PageHelper.startPage(skuPageDto.getPageIndex(),skuPageDto.getPageSize(),skuPageDto.getSkuName());
        List<SkuVo> skuList = skuMapper.getList(skuPageDto);
        skuList.forEach(skuVo -> {
            skuVo.setSkuClass(skuClassMapper.getByClassId(skuVo.getClassId()));
        });
        Page<SkuVo> page = (Page<SkuVo>) skuList;
        return new PageBean(page.getPageNum(),page.getPageSize(), (long) page.getPages(),page.getTotal(),page.getResult());
    }

    @Override
    public Boolean update(Sku sku) {
        Boolean result = skuMapper.update(sku);
        return result;
    }
}
